package com.sxran.im.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.dao.ImGroupMapper;
import com.sxran.im.dao.ImUserGroupMapper;
import com.sxran.im.dao.ImUserMapper;
import com.sxran.im.info.ImGroup;
import com.sxran.im.info.ImUser;
import com.sxran.im.info.ImUserGroup;
import com.sxran.im.service.UserGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LoginCmdHandler extends UserInfoCmdHandler{

    private Logger logger = LoggerFactory.getLogger(LoginCmdHandler.class);

    @Resource
    private ImUserMapper imUserMapper;
    @Resource
    private ImGroupMapper imGroupMapper;

    @Resource
    private UserGroupService userGroupService;



    @Override
    public Object doBeanService(ImUser bean) {
        ImUser imUser = (ImUser) super.doBeanService(bean);

        String userId = imUser.getUserId();
//        // 用户基本信息
//        ImUser imUser = imUserMapper.selectById(userId);
//        // 朋友
//        List<ImGroup> friends = new ArrayList<>();
//        imUser.setFriends(friends);
        // 群组
        List<ImUserGroup> imUserGroups = userGroupService.selectList(userId);
        //
        List<ImGroup> groups = imUserGroups.stream().map(x -> imGroupMapper.selectById(x.getGid())).collect(Collectors.toList());
        imUser.setGroups(groups);
        // 设置状态为上线。
        imUser.setStatus(0);
        imUserMapper.updateById(imUser);
        return imUser;
    }
}
